package com.cheng.dreams.sample.ui.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import com.cheng.dreams.sample.entity.request.Request;
import com.cheng.dreams.sample.util.DialogUtil;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;


/**
 * CreateTime: 2016-04-11上午9:48
 * Author: wjkjinke00@126.com
 * Description:
 */
public class BaseActivity extends AppCompatActivity {

    protected Context mContext;
    private ProgressDialog mProgressDialog;
    private AlertDialog mAlertDialog;
    private Toast mToast;
    protected ArrayList<Request> mRequests;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = this;
        Log.d("onCreate -> ", getClass().getSimpleName());
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.d("onStart -> ", getClass().getSimpleName());
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("onResume -> ", getClass().getSimpleName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("onPause -> ", getClass().getSimpleName());
    }


    public void showToast(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("onStop -> ", getClass().getSimpleName());
    }

    @Override
    protected void onDestroy() {
        hideDialog();
        super.onDestroy();
        Log.d("onDestroy -> ", getClass().getSimpleName());
        if (mRequests != null) {
            for (Request request : mRequests) {
                Disposable disposable = request.getDisposable();
                if (disposable != null && !disposable.isDisposed()) {
                    disposable.dispose();
                }
            }
        }
    }

    /**
     * 将Request加入管理队列,退出时取消订阅
     *
     * @param request
     */
    public void addRequest(Request request) {
        if (mRequests == null) {
            mRequests = new ArrayList<>();
            mRequests.add(request);
        }
    }

    protected boolean isLogin() {
//        String token = UserInfoManager.getInstance().getToken();
//        if (StringUtil.isEmpty(token)) {
//            Toast.makeText(mContext, "请登录!", Toast.LENGTH_SHORT).show();
//            startActivity(new Intent(mContext, LoginActivity.class));
//            return false;
//        }
        return true;
    }

    protected void showProgressDialog() {
        showProgressDialog("加载中,请稍后...");
    }

    protected void showProgressDialog(String message) {
        showProgressDialog(message, true);
    }

    protected void showProgressDialog(String message, boolean cancelable) {
        if (mProgressDialog == null)
            mProgressDialog = DialogUtil.createProgressDialog(mContext, message, cancelable);
        if (mProgressDialog.isShowing()) {
            hideDialog();
            return;
        } else {
            mProgressDialog.show();
        }
    }

    protected void hideDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }

        if (mAlertDialog != null) {
            mAlertDialog.dismiss();
            mAlertDialog = null;
        }
    }


    /**
     * 带取消和确认的对话框
     *
     * @param title
     * @param message
     * @param listener
     */
    protected void showAlertDialog(String title, String message
            , DialogUtil.DialogButtonClickListener listener) {
        showAlertDialog(title, message, "确认", "取消", true, listener);
    }

    /**
     * 对话框,不需要的参数传null
     *
     * @param title
     * @param message
     * @param positive
     * @param negative
     * @param cancelable
     * @param listener
     */
    protected void showAlertDialog(String title, String message, String positive, String negative
            , boolean cancelable, DialogUtil.DialogButtonClickListener listener) {
        mAlertDialog = DialogUtil.createAlertDialog(mContext, title, message, positive, negative, cancelable, listener);
        mAlertDialog.show();
    }
}
